<?php
/**
 * Class for work with database table "mail_letters"
 * 
 * @version 2012-10-22
 * @package Db model
 * @subpackage mail_letters
 * @uses Db_Table
 * @category Db model
 */

class LettersModel extends Db_Table{

	/**
     * __construct
     * This construct function connects to the table "Mail_letters".
     *
     * @version 2012-10-10
     * @uses parent::__construct('mail_letters')
     * @return object
     */
	function __construct()
	{
		parent::__construct("mail_letters");
	}


    /**
	 * getAll
     * Gets all email templates with variables
     *
     * @version 2012-11-06
     * @return array
     * @access public 
     */
    function getAll()
    {
        $sql = 'SELECT * '
             . ' FROM ' . $this->table
             . ' WHERE code != "empty"'
             . ' ORDER BY id';
        $letters = $this->db->getRows($sql, true, 'id');

        $sql = 'SELECT * '.
               ' FROM '._t('mail_vars').
               ' ORDER BY title';
        $vars = $this->db->getRows($sql);
        foreach ($vars as $v)
        {
            if (isset($letters[$v['letter_id']]))
            {
                if (empty($letters[$v['letter_id']]['vars']))
                    $letters[$v['letter_id']]['vars'] = array();
                $letters[$v['letter_id']]['vars'][] = $v;
            }
        }
        return $letters;
    }
}
?>